package dobest.ray.com.jiongjiong.views.ChangeCurve;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dobest.ray.com.jiongjiong.R;

/**
 * Created by Ray on 2016/4/2.
 * https://github.com/ray0807
 * 用于动态显示发育期
 */
public class ChangeCurveView extends View {

    private int width = 0;
    private int height = 0;
    private Paint paintTextBottom = null;//画底部字的画笔
    private Paint paintTextTop = null;//画顶部字的画笔
    private Paint paintCood = null;//画坐标系的画笔
    private Paint paintPoint = null;//画点的画笔
    private Paint paintAdolescent = null;//青春期发育起点线的画笔
    private Context context;
    private int mTextStrokeWidth = 2;
    private int OFFSET = 20;
    private float surplusX = 0;//剩余的X  真正画图的X距离
    private float surplusY = 0;//剩余的Y  真正画图的Y距离
    private float heightOfEachSection = 0;//每节的高度
    private float widthOfEachSection = 0;//每节的宽度

    private float startX = 0;//画点起始点横坐标
    private float startY = 0;//画点起始点纵坐标

    private int distanceFromTextToLine = 10;//字到线的距离
    private int distanceFromBottom = 20;//字到线的距离

    private float redLineX = 0;

    private boolean isFirst = true;
    private List<Integer> points = new ArrayList<>();//数据
    private int max;//最大值
    private int min;//最小值

    private Bitmap bp;


    public ChangeCurveView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public ChangeCurveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public ChangeCurveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {

        paintTextBottom = new Paint();
        paintTextBottom.setAntiAlias(true);
        paintTextBottom.setTextSize(30);
        paintTextBottom.setStyle(Paint.Style.FILL);
        paintTextBottom.setColor(context.getResources().getColor(R.color.text_shallow_gray));
        paintTextBottom.setStrokeWidth(mTextStrokeWidth);

        paintTextTop = new Paint();
        paintTextTop.setAntiAlias(true);
        paintTextTop.setTextSize(30);
        paintTextTop.setStyle(Paint.Style.FILL);
        paintTextTop.setColor(context.getResources().getColor(R.color.colorPrimary));
        paintTextTop.setStrokeWidth(mTextStrokeWidth);

        paintCood = new Paint();
        paintCood.setAntiAlias(true);
        paintCood.setStyle(Paint.Style.FILL);
        paintCood.setColor(context.getResources().getColor(R.color.text_shallow_gray));
        paintCood.setStrokeWidth(1);

        paintAdolescent = new Paint();
        paintAdolescent.setAntiAlias(true);
        paintAdolescent.setStyle(Paint.Style.FILL);
        paintAdolescent.setColor(context.getResources().getColor(R.color.colorAccent));
        paintAdolescent.setStrokeWidth(3);

        paintPoint = new Paint();
        paintPoint.setAntiAlias(true);
        paintPoint.setStyle(Paint.Style.FILL);
        paintPoint.setColor(context.getResources().getColor(R.color.colorPrimary));
        paintPoint.setStrokeWidth(3);
        bp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.img_draw_carve_point);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 父容器传过来的宽度的值
        width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft()
                - getPaddingRight();
        // 父容器传过来的高度的值
        height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingLeft()
                - getPaddingRight();

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawText(canvas);
        drawCoordinate(canvas);
        drawLine(canvas);
        super.onDraw(canvas);
    }


    //画字
    private void drawText(Canvas canvas) {
        canvas.drawText("50%", 0f, height / 2.0f, paintTextBottom);
        surplusX = width - paintTextBottom.measureText("50%") - 2 * distanceFromTextToLine - paintTextBottom.measureText("年龄");
        surplusY = (height - 2 * distanceFromBottom + (paintTextBottom.ascent() + paintTextBottom.descent())) - (2 * distanceFromBottom - (paintTextBottom.ascent() + paintTextBottom.descent()));
        //画底部汉字
        canvas.drawText("3岁", paintTextBottom.measureText("50%") + distanceFromTextToLine, height - distanceFromBottom, paintTextBottom);
        canvas.drawText("6岁", paintTextBottom.measureText("50%") + distanceFromTextToLine + surplusX / 2.0f - paintTextBottom.measureText("6岁") / 2.0f, height - distanceFromBottom, paintTextBottom);
        canvas.drawText("18岁", paintTextBottom.measureText("50%") + distanceFromTextToLine + surplusX - paintTextBottom.measureText("18岁"), height - distanceFromBottom, paintTextBottom);

        //画上面汉字
        canvas.drawText("发育水平", paintTextBottom.measureText("50%") + distanceFromTextToLine - paintTextBottom.measureText("发育水平") / 2.0f, distanceFromBottom - (paintTextBottom.ascent() + paintTextBottom.descent()), paintTextBottom);

        //画最右边的汉字
        canvas.drawText("年龄", width - paintTextBottom.measureText("年龄"), height - 2 * distanceFromBottom + (paintTextBottom.ascent() + paintTextBottom.descent()) / 2.0f, paintTextBottom);

        //画最上面最中间的汉字
        canvas.drawText("当前发育水平", paintTextBottom.measureText("50%") + distanceFromTextToLine + surplusX / 2.0f - paintTextTop.measureText("当前发育水平") / 2.0f, distanceFromBottom - (paintTextBottom.ascent() + paintTextBottom.descent()), paintTextTop);
    }

    //画坐标系
    private void drawCoordinate(Canvas canvas) {
        startX = paintTextBottom.measureText("50%") + distanceFromTextToLine;
        startY = height - 2 * distanceFromBottom + (paintTextBottom.ascent() + paintTextBottom.descent());
        //纵坐标
        canvas.drawLine(startX, height - 2 * distanceFromBottom + (paintTextBottom.ascent() + paintTextBottom.descent()), startX, 2 * distanceFromBottom - (paintTextBottom.ascent() + paintTextBottom.descent()), paintCood); //纵坐标
        //横坐标
        canvas.drawLine(startX, startY, width - paintTextBottom.measureText("年龄") - distanceFromTextToLine, startY, paintCood);


    }

    //划线部分
    private void drawLine(Canvas canvas) {


        //划线
        if (points != null && points.size() > 0) {
            int len = points.size();
            if (max - min > 0) {
                heightOfEachSection = surplusY / max;
                widthOfEachSection = surplusX / (len - 1);
            }
            for (int i = 0; i < len - 1; i++) {
                canvas.drawLine(startX + i * widthOfEachSection, startY - points.get(i) * heightOfEachSection, startX + (i + 1) * widthOfEachSection, startY - points.get(i + 1) * heightOfEachSection, paintPoint);
            }
        }
        //青春期发育起始点位置

        if (redLineX < startX) {
            redLineX = startX;
        }
        if (redLineX > width - paintTextBottom.measureText("年龄") - distanceFromTextToLine) {
            redLineX = width - paintTextBottom.measureText("年龄") - distanceFromTextToLine;
        }
        if (isUp) {
            float tem = ((redLineX - startX) / widthOfEachSection);
            int rint = (int) Math.rint(tem);
            canvas.drawLine(startX + rint * widthOfEachSection, height - 2 * distanceFromBottom + (paintTextBottom.ascent() + paintTextBottom.descent()), startX + rint * widthOfEachSection, 2 * distanceFromBottom - (paintTextBottom.ascent() + paintTextBottom.descent()), paintAdolescent);
            redLineX = startX + rint * widthOfEachSection;
            canvas.drawBitmap(bp, startX + rint * widthOfEachSection - bp.getWidth() / 2.0f, startY - points.get(rint) * heightOfEachSection - bp.getHeight() / 2.0f, null);
        } else {
            if (isFirst) {
                isFirst = false;
                redLineX = startX + points.size() / 2 * widthOfEachSection;
                canvas.drawLine(startX + points.size() / 2 * widthOfEachSection, height - 2 * distanceFromBottom + (paintTextBottom.ascent() + paintTextBottom.descent()), startX + points.size() / 2 * widthOfEachSection, 2 * distanceFromBottom - (paintTextBottom.ascent() + paintTextBottom.descent()), paintAdolescent);
            }
            canvas.drawLine(redLineX, height - 2 * distanceFromBottom + (paintTextBottom.ascent() + paintTextBottom.descent()), redLineX, 2 * distanceFromBottom - (paintTextBottom.ascent() + paintTextBottom.descent()), paintAdolescent);
            float tem = ((redLineX - startX) / widthOfEachSection);
            int rint = (int) Math.rint(tem);
            canvas.drawBitmap(bp, startX + rint * widthOfEachSection - bp.getWidth() / 2.0f, startY - points.get(rint) * heightOfEachSection - bp.getHeight() / 2.0f, null);
        }

    }

    private boolean isCanMove = false;
    private boolean isUp = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (redLineX < event.getX() + OFFSET && redLineX > event.getX() - OFFSET) {
                    isCanMove = true;
                }
                return true;
            case MotionEvent.ACTION_MOVE:
                if (isCanMove) {
                    isUp = false;
                    redLineX = event.getX();
                    postInvalidate();
                }
                break;
            case MotionEvent.ACTION_UP:
                isUp = true;
                if (isCanMove) {
                    postInvalidate();
                }
                isCanMove = false;
                break;
        }
        return super.onTouchEvent(event);
    }

    //设置数据
    public void setData(List<Integer> points) {
        this.points = points;
        isFirst = true;
        max = Collections.max(points);
        min = Collections.min(points);
        postInvalidate();
    }
}
